

## FIGURE 1

# load, combine data
library(rio)
library(tidyverse)

# generate maps
library(rgdal)

# nice color schemes
library(viridis)

# combine maps
library(gridExtra)




# LOAD DATA
all_countries <- import("all_measures_world.csv") %>%
  filter(is.na(`Year end`)) %>%
  select(Country,State,Mechanism) %>%
  unique()


# load world map
world <- map_data("world")
world <- world %>%
  filter(region != "Antarctica")

# load map of US states
states <- map_data("state")

# add map of Canadian and Australian states
other_maps <- readOGR("ne_50m_admin_1_states_provinces_lakes", 'ne_50m_admin_1_states_provinces_lakes', encoding='UTF-8')
canada <- subset(other_maps, name %in% c("Alberta", "Northwest Territories"))
australia <- subset(other_maps, name %in% c("Western Australia"))

us <- subset(states, region %in% c("alabama","idaho","louisiana","montana","new mexico",
                                   "north dakota","utah","texas","west virginia","wyoming"))

alaska <- world %>%
  filter(subregion=="Alaska")

## FUNDS
funds <- all_countries %>%
  filter(Mechanism=="Fund" & State=="") %>%
  unique() %>%
  rename(region = Country) %>%
  select(region) %>%
  mutate(fund = 1)

funds_map <- world %>%
  full_join(funds) %>% 
  ggplot(aes(long, lat, group = group)) +
  geom_polygon(aes(fill = fund), size = 0.001) +
  geom_polygon(data = canada, fill = "#21908CFF", size = 0.001) + 
  geom_polygon(data = australia, fill = "#21908CFF", size = 0.001) + 
  geom_polygon(data = us, fill = "#21908CFF", size = 0.001) + 
  geom_polygon(data = alaska, fill = "#21908CFF", size = 0.001) + 
  theme_void() + 
  theme(plot.margin = unit(c(0,0,0,0), "cm")) +
  scale_fill_viridis(na.value = "gray92") +
  theme(legend.position = "none") + labs(title="Natural Resource Funds") + 
  theme(plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(size=16,hjust = 0.5))


## FISCAL RULES
rules <- all_countries %>%
  filter(Mechanism=="Fiscal rule" & State=="") %>%
  unique() %>%
  rename(region = Country) %>%
  select(region) %>%
  mutate(rule = 1,
         region = ifelse(region=="United States", "USA", region))

rules_map <- world %>%
  full_join(rules) %>% 
  ggplot(aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rule), size = 0.001) +
  geom_polygon(data = canada, fill = "#21908CFF", size = 0.001) +
  geom_polygon(data = alaska, fill = "#21908CFF", size = 0.001) + 
  theme_void() + 
  theme(plot.margin = unit(c(0,0,0,0), "cm")) +
  scale_fill_viridis(na.value = "gray92") +
  theme(legend.position = "none") + labs(title="Fiscal Rules") + 
  theme(plot.margin = unit(c(0,0,0,0), "cm"),
        plot.title = element_text(size=16,hjust = 0.5))


maps <- grid.arrange(funds_map,rules_map, ncol=1)

# ggsave("figure12.pdf", width = 8, height = 10, maps)
